# -*- coding: utf-8 -*-

from django.db import models
from django.contrib.auth.models import  User
from django.core.urlresolvers import reverse


class Category(models.Model):
    """ 类别表，用来存储每次活动的类别
    """
    name = models.CharField(u'类别名', max_length=20)
    description = models.CharField(u'类别描述', max_length=255, blank=True, null=True)
    create_at = models.DateTimeField(u'创建时间', auto_now_add=True)
    picture = models.ImageField(
        u"类别图片",
        upload_to='category/%Y-%m-%d',
        default='',
        blank=True)
    user = models.ForeignKey(User)

    class Meta:
        verbose_name_plural = u'类别'

    def __unicode__(self):
        return 'name:%s' % self.name

    def get_absolute_url(self):
        return reverse('category-detail', kwargs={'pk': self.id})

    def save(self):

        super(Category, self).save()


# todo http://v.juhe.cn/postcode/pcd?key=fcdbb0745337c7a81bb0840df124d9e9
class Contact(models.Model):
    """ 联系人表,用于存储所有联系人基本信息
    """
    name = models.CharField(u'姓名', max_length=30)
    phone = models.CharField(u'手机号', max_length=11, blank=True, null=True)
    email = models.EmailField(u'电子邮件', max_length=40, blank=True, null=True)
    country = models.CharField(u'国家', max_length=10, default=u'中国')
    province = models.CharField(u'省', max_length=10, default=u'北京市')
    city = models.CharField(u'城市', max_length=10, default=u'北京市')
    district = models.CharField(u'区', max_length=20)
    street = models.CharField(u'街道', max_length=100)
    postcode = models.IntegerField(u'邮编', max_length=6, null=True, blank=True)
    create_at = models.DateTimeField(u'创建时间', auto_now_add=True)

    class Meta:
        verbose_name_plural = u'联系人'

    def __unicode__(self):
        return 'name:%s' % self.name


class ContactKeeper(models.Model):
    """ 存储联系人,用于记录自己的联系地址
    """

    contact = models.ForeignKey(Contact)
    owner = models.ForeignKey(User)
    create_at = models.DateTimeField(u'创建时间', auto_now_add=True)

    class Meta:
        verbose_name_plural = u'存储联系人'

    def __unicode__(self):
        return 'contact:%s' % self.contact.name

    def get_absolute_url(self):
        return reverse('contactkeeper-detail', kwargs={'pk': self.id})


class ContactManager(models.Model):
    """ 管理联系人,用于记录category收集的联系人地址
    """
    category = models.ForeignKey(Category)
    contact = models.ForeignKey(Contact)
    user = models.ForeignKey(User)
    create_at = models.DateTimeField(u'创建时间', auto_now_add=True)
    status = models.BooleanField(u'状态', default=False)

    class Meta:
        verbose_name_plural = u'管理联系人'

    def __unicode__(self):
        return 'contact:%s' % self.contact.name




